//
//  GEProgressBarTableViewCell.m
//  ERP
//
//  Created by goldeney on 15/3/16.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "GEProgressBarTableViewCell.h"
#import "RTSpinKitView.h"
#import "NSDictionary+DataFormat.h"

#import "GEObjectButton.h"
#import "GetUserStationService.h"

@interface GEProgressBarTableViewCell()

@property (nonatomic, assign) BOOL isAlreadyGetData;
@property (nonatomic, assign) BOOL isGetDataFail;

@property (nonatomic, strong) UIView *drawView;

@end

@implementation GEProgressBarTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        CGRect custom = self.contentView.frame;
        custom.size.width = SuperWidth;
        custom.size.height = 240;
        self.frame = custom;
        
        // Initialization code
        [self drawProgressBarTableView:nil];
    }
    return self;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier height:(CGFloat)height
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        CGRect custom = self.contentView.frame;
        custom.size.width = SuperWidth;
        custom.size.height = height;
        self.frame = custom;
        
        // Initialization code
        [self drawProgressBarTableView:nil];
    }
    return self;
}

- (void)setCellHeight:(NSInteger)cellHeight
{
    _cellHeight = cellHeight;
    
    CGRect custom = self.contentView.frame;
    custom.size.height = cellHeight;
    self.frame = custom;
    
    //从新绘制
    [self drawProgressBarTableView:nil];
}

- (void)setFrame:(CGRect)frame
{
    if(self.isLeftRightPadding){
        frame.origin.x += 8;
        frame.size.width = SuperWidth - 16;
    }
    [super setFrame:frame];
}

//绘制进度条
- (void)drawProgressBarTableView:(UITableView*)tableView
{
    if(self.drawView) [self.drawView removeFromSuperview];
    
    RTSpinKitView *spinner = [[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStyleFadingCircleAlt];
    spinner.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    [spinner startAnimating];
    [self.contentView addSubview:spinner];
    
    self.drawView = spinner;
}

//绘制错误页面
- (void)drawErrorViewTableView:(UITableView*)tableView
{
    if(self.drawView) [self.drawView removeFromSuperview];
    CGRect viewFrame = self.bounds;
    UIView *view = [[UIView alloc] initWithFrame:viewFrame];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"not_error_bg"]];
    CGFloat imageViewWidth = imageView.frame.size.width;
    CGFloat imageViewHeight = imageView.frame.size.height;
    imageView.frame = CGRectMake(view.frame.size.width/2 - imageViewWidth/2, view.frame.size.height/2 - imageViewHeight/2 - 15,imageViewWidth,imageViewHeight);
    [view addSubview:imageView];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(imageView.frame.origin.x - 5, imageView.frame.origin.y + imageViewHeight + 4, imageViewWidth + 10, 26)];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.font = [UIFont systemFontOfSize:15];
    lable.textColor = [UIColor colorWithRed:217/255.0 green:217/255.0 blue:217/255.0 alpha:1];
    lable.text = @"加载失败了";
    [view addSubview:lable];
    
    GEObjectButton *button = [[GEObjectButton alloc] initWithFrame:viewFrame];
    button.object = tableView;
    [button addTarget:self action:@selector(againLoadEvent:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    [self.contentView addSubview:view];
    self.drawView = view;
    [tableView reloadData];
}

//绘制加载失败页面
- (void)drawLoadingFailViewTableView:(UITableView*)tableView
{
    if(self.drawView) [self.drawView removeFromSuperview];
    CGRect viewFrame = self.bounds;
    UIView *view = [[UIView alloc] initWithFrame:viewFrame];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"not_network_bg"]];
    CGFloat imageViewWidth = imageView.frame.size.width;
    CGFloat imageViewHeight = imageView.frame.size.height;
    imageView.frame = CGRectMake(view.frame.size.width/2 - imageViewWidth/2, view.frame.size.height/2 - imageViewHeight/2 - 15,imageViewWidth,imageViewHeight);
    [view addSubview:imageView];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(imageView.frame.origin.x - 5, imageView.frame.origin.y + imageViewHeight + 4, imageViewWidth + 10, 26)];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.font = [UIFont systemFontOfSize:15];
    lable.textColor = [UIColor colorWithRed:217/255.0 green:217/255.0 blue:217/255.0 alpha:1];
    lable.text = @"无网络连接";
    [view addSubview:lable];
    
    GEObjectButton *button = [[GEObjectButton alloc] initWithFrame:viewFrame];
    button.object = tableView;
    [button addTarget:self action:@selector(againLoadEvent:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    [self.contentView addSubview:view];
    self.drawView = view;
    [tableView reloadData];
}

//绘制无数据页面
- (void)drawNotDataViewTableView:(UITableView*)tableView
{
    if(self.drawView) [self.drawView removeFromSuperview];
    CGRect viewFrame = self.bounds;
    UIView *view = [[UIView alloc] initWithFrame:viewFrame];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"not_chart_bg"]];
    CGFloat imageViewWidth = imageView.frame.size.width;
    CGFloat imageViewHeight = imageView.frame.size.height;
    imageView.frame = CGRectMake(view.frame.size.width/2 - imageViewWidth/2, view.frame.size.height/2 - imageViewHeight/2 - 15,imageViewWidth,imageViewHeight);
    [view addSubview:imageView];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y + imageViewHeight + 4, imageViewWidth, 26)];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.font = [UIFont systemFontOfSize:15];
    lable.textColor = [UIColor colorWithRed:217/255.0 green:217/255.0 blue:217/255.0 alpha:1];
    lable.text = @"暂无数据";
    [view addSubview:lable];
    
    GEObjectButton *button = [[GEObjectButton alloc] initWithFrame:viewFrame];
    button.object = tableView;
    [button addTarget:self action:@selector(againLoadEvent:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    [self.contentView addSubview:view];
    self.drawView = view;
    [tableView reloadData];
}

//重新加载
- (void)againLoadEvent:(GEObjectButton*)button
{
    UITableView *tableView = (UITableView*)button.object;
    self.isAlreadyGetData = NO;
    [self drawProgressBarTableView:tableView];
    [tableView reloadData];
}

//获取网络数据(普通)
- (void)getNetWorkDataTableView:(UITableView*)tableView moduleType:(NSString*)module parameter:(NSDictionary*)paramet index:(NSInteger)index
{
    //是否已经拉取过数据
    if (self.isAlreadyGetData) return;
    else self.isAlreadyGetData = YES;
    __weak __typeof(tableView) weakSelf = tableView;
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeERP];
    [content setModuleType:module];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    //设置站点ID
    StationInfoEntity *model = [[GetUserStationService sharedService] getSelectStationEntity];
    [content setStationID:[model.stationId integerValue]];
    
    if (paramet)
    for(NSString *key in paramet){
        id value = [paramet objectFormatForKey:key];
        [content addParameterKey:key value:value];
    }
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(result.resultArray && result.resultArray.count>0){
            self.objectValue = result.resultArray;
            if (weakSelf) {
                __strong __typeof(weakSelf) strongSelf = weakSelf;
                NSIndexSet * sectionToReload = [[NSIndexSet alloc] initWithIndex:index];
                NSInteger row = [strongSelf.dataSource tableView:strongSelf numberOfRowsInSection:index];
                if(strongSelf && row >0)
                [strongSelf reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationLeft];
            }
        } else {
            [self drawNotDataViewTableView:tableView];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"==failed===%@",error);
        [self drawErrorViewTableView:tableView];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"===abnormal==%@",error);
        [self drawLoadingFailViewTableView:tableView];
    }];
}

//获取网络数据(集团)
- (void)getNetWorkGroupDataTableView:(UITableView*)tableView moduleType:(NSString*)module parameter:(NSDictionary*)paramet index:(NSInteger)index
{
    //是否已经拉取过数据
    if (self.isAlreadyGetData) return;
    else self.isAlreadyGetData = YES;
    __weak __typeof(tableView) weakSelf = tableView;
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:module];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    if (paramet)
        for(NSString *key in paramet){
            id value = [paramet objectFormatForKey:key];
            [content addParameterKey:key value:value];
        }
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(result.resultArray && result.resultArray.count>0){
            self.objectValue = result.resultArray;
            if (weakSelf) {
                __strong __typeof(weakSelf) strongSelf = weakSelf;
                NSIndexSet * sectionToReload = [[NSIndexSet alloc] initWithIndex:index];
                NSInteger row = [strongSelf.dataSource tableView:strongSelf numberOfRowsInSection:index];
                if(strongSelf && row >0)
                    [strongSelf reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationLeft];
            }
        } else {
            [self drawNotDataViewTableView:tableView];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"==failed===%@",error);
        [self drawErrorViewTableView:tableView];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"===abnormal==%@",error);
        [self drawLoadingFailViewTableView:tableView];
    }];
}

//获取网络数据(OA)
- (void)getNetWorkOADataTableView:(UITableView*)tableView moduleType:(NSString*)module parameter:(NSDictionary*)paramet index:(NSInteger)index
{
    //是否已经拉取过数据
    if (self.isAlreadyGetData) return;
    else self.isAlreadyGetData = YES;
    __weak __typeof(tableView) weakSelf = tableView;
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeOA];
    [content setModuleType:module];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    if (paramet)
        for(NSString *key in paramet){
            id value = [paramet objectFormatForKey:key];
            [content addParameterKey:key value:value];
        }
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(result.resultArray && result.resultArray.count>0){
            self.objectValue = result.resultArray;
            if (weakSelf) {
                __strong __typeof(weakSelf) strongSelf = weakSelf;
                NSIndexSet * sectionToReload = [[NSIndexSet alloc] initWithIndex:index];
                NSInteger row = [strongSelf.dataSource tableView:strongSelf numberOfRowsInSection:index];
                if(strongSelf && row >0)
                    [strongSelf reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationLeft];
            }
        } else {
            [self drawNotDataViewTableView:tableView];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"==failed===%@",error);
        [self drawErrorViewTableView:tableView];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"===abnormal==%@",error);
        [self drawLoadingFailViewTableView:tableView];
    }];
}

//获取网络数据（报表）
- (void)getNetWorkReportFormTableView:(UITableView*)tableView moduleType:(NSString*)module parameter:(NSDictionary*)paramet index:(NSInteger)index
{
    //是否已经拉取过数据
    if (self.isAlreadyGetData) return;
    else self.isAlreadyGetData = YES;
    __weak __typeof(tableView) weakSelf = tableView;
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeERP];
    [content setModuleType:module];
    [content setResultBackType:ResultBackTypeReportForms];
    
    if (paramet)
        for(NSString *key in paramet){
            id value = [paramet objectFormatForKey:key];
            [content addParameterKey:key value:value];
        }
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(result.resultArray && result.resultArray.count>0){
            self.objectValue = result.resultArray;
            if (weakSelf) {
                __strong __typeof(weakSelf) strongSelf = weakSelf;
                NSIndexSet * sectionToReload = [[NSIndexSet alloc] initWithIndex:index];
                NSInteger row = [strongSelf.dataSource tableView:strongSelf numberOfRowsInSection:index];
                if (strongSelf && row >0) [strongSelf reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationLeft];
            }
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"==failed===%@",error);
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"===abnormal==%@",error);
    }];
}

#pragma mark -
#pragma mark V5.0.0及以后版本调用

//获取网络数据(V5.0.0及以后)
- (void)getNetWorkDataTableView:(UITableView*)tableView moduleType:(NSString*)module parameter:(NSDictionary*)paramet StationID:(NSInteger)stationID index:(NSInteger)index
{
    //是否已经拉取过数据
    if (self.isAlreadyGetData) return;
    else self.isAlreadyGetData = YES;
    __weak __typeof(tableView) weakSelf = tableView;
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeERP];
    [content setModuleType:module];
    [content setStationID:stationID];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    if (paramet)
        for(NSString *key in paramet){
            id value = [paramet objectFormatForKey:key];
            [content addParameterKey:key value:value];
        }
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(result.resultArray && result.resultArray.count>0){
            self.objectValue = result.resultArray;
            if (weakSelf) {
                __strong __typeof(weakSelf) strongSelf = weakSelf;
                NSIndexSet * sectionToReload = [[NSIndexSet alloc] initWithIndex:index];
                NSInteger row = [strongSelf.dataSource tableView:strongSelf numberOfRowsInSection:index];
                if(strongSelf && row >0)
                    [strongSelf reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationLeft];
            }
        } else {
            [self drawNotDataViewTableView:tableView];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"==failed===%@",error);
        [self drawErrorViewTableView:tableView];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"===abnormal==%@",error);
        [self drawLoadingFailViewTableView:tableView];
    }];
}

//获取网络数据(V5.0.0及以后)
- (void)getNetWorkDataTableView:(UITableView*)tableView moduleType:(NSString*)module parameter:(NSDictionary*)paramet StationID:(NSInteger)stationID interfaceType:(InterfaceType)interfaceType index:(NSInteger)index
{
    //是否已经拉取过数据
    if (self.isAlreadyGetData) return;
    else self.isAlreadyGetData = YES;
    __weak __typeof(tableView) weakSelf = tableView;
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:interfaceType];
    [content setModuleType:module];
    [content setStationID:stationID];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    if (paramet)
        for(NSString *key in paramet){
            id value = [paramet objectFormatForKey:key];
            [content addParameterKey:key value:value];
        }
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(result.resultArray && result.resultArray.count>0){
            self.objectValue = result.resultArray;
            if (weakSelf) {
                __strong __typeof(weakSelf) strongSelf = weakSelf;
                NSIndexSet * sectionToReload = [[NSIndexSet alloc] initWithIndex:index];
                NSInteger row = [strongSelf.dataSource tableView:strongSelf numberOfRowsInSection:index];
                if(strongSelf && row >0)
                    [strongSelf reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationLeft];
            }
        } else {
            [self drawNotDataViewTableView:tableView];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"==failed===%@",error);
        [self drawErrorViewTableView:tableView];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"===abnormal==%@",error);
        [self drawLoadingFailViewTableView:tableView];
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
